package symptomsSearch;

import java.sql.*;
import ctm.javacode.dbBean;

import javax.naming.NamingException;

/*
 * 檔名 : Segmentation.java
 
 * 函式 :OutputSegmentationSymptoms()
 * 功能 :原始症狀斷詞
 * 參數 :OriginalSymptoms 為原始症狀字串 。例 :小便大便痛
 * 回傳 :SplitSymptoms 為 斷詞後原始症狀字串。例 :小便-大便-痛 
 
 */

public class Segmentation {
	

	public String OutputSegmentationSymptoms(String OriginalSymptoms)
			throws NamingException, SQLException {
		/*
		 * 連結資料庫設定
		 */
		dbBean db = new dbBean("jdbc/ctmpllab");
		db.Connected();

		String brffer = null; /*儲存還未被斷詞之原始症狀字串*/
		String SplitSymptoms = null; /*儲存還已被斷詞之原始症狀字串*/

		for (int text_index = 0; text_index < OriginalSymptoms.length(); text_index++) {

			brffer = brffer + OriginalSymptoms.charAt(text_index); /*由左至右逐一讀入原始症狀字元*/

			/*
			 * synonym_table 為同義詞資料表，儲存在資料庫
			 * 
			 * 查詢資料表裡是否存在以brffer為開頭之關鍵字，若存在，繼續加入下一個字元
			 */
			String sqlQuery = "Select * from synonym_table where (keyword LIKE '"+ brffer + "%')";
			ResultSet rs;
			rs = db.ExcuteQuery(sqlQuery);

			
			
			if (!rs.next()) { 
				

				SplitSymptoms = SplitSymptoms
						+ brffer.substring(0, brffer.length() - 1) + "-";

				brffer = brffer.substring(brffer.length() - 1);

			}
			if (text_index == (OriginalSymptoms.length() - 1)) {

				SplitSymptoms = SplitSymptoms + brffer;

			}

		}

		db.CloseConn();
		
		return SplitSymptoms;

		
		
	}

}
